package com.niuquan.activitie;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.alibaba.sdk.android.util.JSONUtils;
import com.niuquan.base_data.Messages;
import com.niuquan.function.findniushi_Nettool;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Handler;

/**
 * Created by DELL on 2015/5/23.
 */
public class NiushiFragment extends Fragment {
    private ArrayList<HashMap<String,Object>> list = null;
    private ArrayList<ArrayList<HashMap<String,Object>>> listoflist = null;
    private ScrollView scrollView = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        scrollView = (ScrollView)this.getView().findViewById(R.id.niushilist);
        StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
        list.add(new BasicNameValuePair("userId", "1"));
        JSONArray array = null;
        try {
            array =  new JSONArray(findniushi_Nettool.send("http://172.27.35.1:8080/expertcircle/recommendMission.action", new Messages(list)));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        updataui(setlistview(array), this.getActivity().getApplicationContext());
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.layout_niushi, container, false);
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }



    private ArrayList<HashMap<String,Object>> setlistview(JSONArray content)
    {

        int num = content.length();
        int i = 0;
        list = new ArrayList<HashMap<String,Object>>();
        while (i<num)
        {
            try {
                JSONObject one = (JSONObject)content.get(i);
                HashMap<String,Object>onemap = new HashMap<String,Object>();
                onemap.put("task_content",one.getString("name"));
                onemap.put("describe",one.getString("description"));
                onemap.put("howmuch",one.getString("getcoin"));
                list.add(onemap);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            i++;
        }
        return list;
    }
    private void updataui(ArrayList<HashMap<String,Object>> list,Context context)
    {
            ListView listView = new ListView(context);
            SimpleAdapter simpleAdapter = new SimpleAdapter(context,list,R.layout.tuijianitem,
            new String[]{"name","description","getcoin"},
            new int[]{R.id.titletask,R.id.task_content,R.id.howmuch});
            listView.setAdapter(simpleAdapter);
            scrollView.addView(listView);
    }
}
